/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2022-06-08
 * Time: 16:15
 */
public class binarySearchTree {

    static class TreeNode{
        public int Key;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int key) {
            Key = key;
        }
    }

    public TreeNode root;

    public boolean insert(int key) {
        TreeNode node = new TreeNode(key);

        if(root == null) {
            root = node;
            return true;
        }

        TreeNode cur = root;
        TreeNode prev = null;
        while(cur != null) {
            if(key < cur.Key) {
                prev = cur;
                cur = cur.left;
            }else if(key > cur.Key) {
                prev = cur;
                cur = cur.right;
            }else {
                return false;
            }
        }

        if(key < prev.Key) {
            prev.left = node;
        }else {
            prev.right = node;
        }

        return true;
    }

    public void inOder(TreeNode root) {
        if(root == null) return;

        inOder(root.left);
        System.out.print(root.Key + " ");
        inOder(root.right);
    }

    public boolean Search(int key) {
        TreeNode cur = root;

        while( cur != null) {
            if(key > cur.Key) {
                cur = cur.right;
            }else if(key < cur.Key) {
                cur = cur.left;
            }else {
                return true;
            }
        }

        return false;
    }

    public boolean remove(int key) {
        TreeNode cur = root;
        TreeNode prev = null;

        while( cur != null) {
            if(key > cur.Key) {
                prev = cur;
                cur = cur.right;
            }else if(key < cur.Key) {
                prev = cur;
                cur = cur.left;
            }else {
                removeNode(cur,prev);
                return true;
            }
        }
        return false;
    }

    private void removeNode(TreeNode cur,TreeNode prev) {
        if(cur.left == null) {
            if(cur == root) {
                root = cur.right;
            }else if(prev.left == cur) {
                prev.left = cur.right;
            }else {
                prev.right = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                root = cur.left;
            }else if(prev.left == cur) {
                prev.left = cur.left;
            }else {
                prev.right = cur.left;
            }
        }else {
            TreeNode prent = cur;
            TreeNode prentCur = cur.right;

            while(prentCur.left != null) {
                prent = prentCur;
                prentCur = prentCur.left;
            }

           /* cur.Key = prentCur.Key;
            if(prent.left == prentCur) {
                prent.left = cur.right;
            }else {
                prent.right = cur.right;
            }*/
            cur.Key = prentCur.Key;
            if(prent.left == prentCur) {
                prent.left = prentCur.right;
            }else {
                prent.right = prentCur.right;
            }
        }
    }
}
